<?php
namespace Admin\Controller;
class JingtaiController extends IsloginController{
    function index(){
        $this->display();
    }


    public function setindexhtml(){

        $host = I("server.HTTP_HOST");

        $url = 'http://'.$host.'/index.php';

        $dir = './HTML';               //存放路径

        $filename = 'index';       //分类名当文件名
        $res = createHtml($url,$dir,$filename);
        if($res){
            $this->ajaxReturn(array("status"=>"1","msg"=>"操作成功"));
        }else{
            $this->ajaxReturn(array("status"=>"0","msg"=>"操作失败"));
        }
    }
    public function sethtml(){
        $_G = F('f_data');
        $id =  I("get.id");
        $m = M('cms_cate');
        if (!$id) {
            $info['status'] = 0;
            $info['msg'] = 'ID不能为空!';
            $this->ajaxReturn($info);
        }
        $catdir = trim($id);
        $map['catdir']=$catdir;
        $cat = $m->where($map)->limit(1)->find();
        if (empty($cat)) {
            $info['status'] = 0;
            $info['msg'] = '操作失败,请检查此分类!';
            $this->ajaxReturn($info);
        }
        $psize = $_G['pagesize']['svalue'];          //每个分页展示几条数据
        $tmap['cateid'] = $cat['id'];
        $tmap['status'] = 1;
        if($cat['model'] == 'artical'){
            $count = M('artical')->where($tmap)->count();//当前分类下文章总数
        }else if($cat['model'] == 'product'){
            $count = M('product')->where($tmap)->count();
        }
        $pagecount = ceil(intval($count)/intval($psize));   //分页数(向上取整)
        $host = I("server.HTTP_HOST");
        //多页面,列表分页
        if($pagecount > 1){
            for($i=1;$i<=$pagecount;$i++){
                $url = 'http://'.$host.'/App/Index/articalList/id/'.$catdir.'/p/'.$i.'';
                if($cat['lv'] == 1){
                    $dir = './HTML/';  //存放路径
                }else if(!empty($cat['pid']) && ($cat['lv'] == 2)){   //当前类有父级分类
                    $pid = $cat['pid'];
                    $tmap['id']=$pid;
                    $pcat = $m->where($tmap)->limit(1)->find();
                    $pcatdir = $pcat['catdir'];
                    $dir = './HTML/'.$pcatdir;    //存放路径
                }
                $filename = $catdir.'_'.$i;           //分类名[当前文件名]
                $res = createHtml($url,$dir,$filename);//创建静态文件
                if(!$res){
                    $this->ajaxReturn(array("status"=>"0","msg"=>"操作失败"));exit();
                }
            }
        }
        //单页面
        $url = 'http://'.$host.'/App/Index/articalList/id/'.$catdir.'';
        if($cat['lv'] == 1){
            $dir = './HTML/';                //存放路径
        }else if(!empty($cat['pid']) && ($cat['lv'] == 2)){
            $pid = $cat['pid'];
            $tmap['id']=$pid;
            $pcat = $m->where($tmap)->limit(1)->find();
            $pcatdir = $pcat['catdir'];
            $dir = './HTML/'.$pcatdir;      //存放路径
        }
        $filename = $catdir;                    //分类名当文件名
        $res = createHtml($url,$dir,$filename);
        if($res){
            $this->ajaxReturn(array("status"=>"1","msg"=>"静态页面创建成功"));
        }else{
            $this->ajaxReturn(array("status"=>"0","msg"=>"操作失败"));
        }
    }
}